/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.tyut.hrs.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.neusoft.tyut.hrs.comon.CommonResult;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

@RestController // @Controller + @ResponseBody
@CrossOrigin("*")
@Slf4j
@RequestMapping("dept")
public class DeptController {
    @PostMapping("/add")
    public CommonResult test(HttpServletRequest request) {
        String token = request.getHeader("Authoriztion");
        WebClient client = WebClient.create("http://localhost:9010/emp/test/token/" + token);
        log.info("add: {}", request.getHeader("Authoriztion"));
        Mono<CommonResult> mono =
                client.get()
                        .acceptCharset(StandardCharsets.UTF_8)
                        .accept(MediaType.TEXT_HTML)
                        .retrieve()
                        .bodyToMono(CommonResult.class);

        CommonResult result = mono.block();
        if (result.getCode() == 200) {
            return CommonResult.success("添加成功");
        } else {
            return CommonResult.fail(result.getCode(), result.getMsg());
        }
    }
}
